package com.yc.sp_index.web;

import com.yc.cloudvideo.entity.Store;
import com.yc.cloudvideo.entity.StoreFile;
import com.yc.cloudvideo.entity.StoreUser;
import com.yc.cloudvideo.entity.User;
import com.yc.sp_index.web.fegin.ISp_store;
import com.yc.sp_index.web.fegin.ISp_storeuser;
import com.yc.sp_index.web.fegin.ISp_user;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RestController
public class IndexAction {
    @Resource
    ISp_store iSpStore;
    @Resource
    ISp_user iSpUser;

    @GetMapping("storedetails")
    public ModelAndView storedetails(
            ModelAndView modelAndView
    ){
        modelAndView.setViewName("storedetails");
        return modelAndView;

    }

    @Resource
    ISp_storeuser iSp_storeuser;

    @GetMapping("{page}.htm")
    ModelAndView toPage(@PathVariable String page, ModelAndView modelAndView){
        modelAndView.setViewName(page);
        return modelAndView;
    }
    //传入仓库id和仓库下的一个路径，获取该路径下的所有内容，路径可以为空
    @RequestMapping(value = "detail",params = {"cid"})
    ModelAndView detail(int cid, @RequestParam(defaultValue = "") String filePath, ModelAndView md){
        List<StoreFile> files = iSpStore.getFiles(cid,filePath);
        //获取路径下的文件
        md.addObject("files",files);
        //查询仓库信息，
        Store store=iSpStore.getStoreByCid(cid);
        //添加到模型中
        md.addObject("store",store);
        //查询仓库拥有者信息
        User user = iSpUser.selectStoreOwner(cid);
        md.addObject("owner",user);


        //设定跳转路径  名字 name，跳转地址 path
        List<Map<String,String>> mapList=new ArrayList<>();
        //filePath不为空时
        if(!filePath.isEmpty()) {
            //根据传进来的filePath进行分割
            String[] split = filePath.split("/");
            for (int i = 0; i < split.length; i++) {
                //添加到mapList集合中
                Map<String, String> map = new HashMap<>();
                //获取展示名
                String name=split[i];
                map.put("name",name);
                //拼接  路径
                StringBuilder path=new StringBuilder("");
                for(int j=0;j<=i;j++){
                    if(j!=0){
                        path.append("/");
                    }
                    path.append(split[j]);
                }
                map.put("path", String.valueOf(path));
                mapList.add(map);
            }

        }
        //目录级别
        md.addObject("paths",mapList);

        md.setViewName("detail");
        return md;
    }

}
